package com.cqjtu.eecs.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cqjtu.eecs.entity.Academy;
import com.cqjtu.eecs.entity.Major;
import com.cqjtu.eecs.entity.params.academy.AcademyAddParams;
import com.cqjtu.eecs.entity.params.academy.AcademyUpdateParams;
import com.cqjtu.eecs.entity.vo.AcademyVO;
import com.cqjtu.eecs.entity.vo.Academy_Major_VO;
import com.cqjtu.eecs.entity.vo.MajorVO2;
import com.cqjtu.eecs.exception.NotFoundException;
import com.cqjtu.eecs.exception.ParamsErrorException;
import com.cqjtu.eecs.service.IAcademyService;
import com.cqjtu.eecs.service.IMajorService;
import com.cqjtu.eecs.utils.Assert;
import com.cqjtu.eecs.utils.Result;
import com.cqjtu.eecs.utils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author luojun
 * @since 2022-02-28
 */
@RestController
@RequestMapping("/academy")
@Api(tags = "学院接口")
public class AcademyController {
    private final IAcademyService academyService;

    private final IMajorService iMajorService;

    public AcademyController(IAcademyService academyService,IMajorService iMajorService) {
        this.academyService = academyService;
        this.iMajorService=iMajorService;
    }

    @PostMapping("/add")
    @ApiOperation("增加一个学院信息")
    public Result<Object> addAcademy(@Valid @RequestBody AcademyAddParams academyAddParams){
        Academy academy = new Academy();
        BeanUtils.copyProperties(academyAddParams, academy); //Params转DO
        if (academyService.save(academy))
            return Result.success();
        return Result.error();
    }

    @PutMapping("/update")
    @ApiOperation("修改学院名")
    public Result<Object> updateAcademy(@Valid @RequestBody AcademyUpdateParams updateParams){
        System.out.println(updateParams);
        Academy academy = new Academy();
        BeanUtils.copyProperties(updateParams, academy); //Params转DO
        if (academyService.updateById(academy))
            return Result.success();
        return Result.error();
    }
    @GetMapping("/academyList")
    @ApiOperation("获取全部的学院信息")
    public List<AcademyVO> getAcademyList(){
        List<Academy> academyList = academyService.list();
        List<AcademyVO> academyVoList = new ArrayList<>();
        academyList.forEach(item->{ // DOList转VOList
            AcademyVO academyVo = new AcademyVO();
            BeanUtils.copyProperties(item, academyVo);
            academyVoList.add(academyVo);
        });
        return academyVoList;
    }
    @GetMapping("/academyById/{id}")
    @ApiOperation("通过学院id获取学院")
    public AcademyVO getAcademyVoById(@PathVariable("id") String id){
        Assert.notNull(id, new ParamsErrorException("id入参不能为null"));
        Assert.notBlank(id, new ParamsErrorException("id入参不能为空"));
        Academy academy = academyService.getById(id);
        Assert.notNull(academy, new NotFoundException("学院id不存在"));
        AcademyVO academyVo = new AcademyVO();
        BeanUtils.copyProperties(academy, academyVo);
        return academyVo;
    }

    @DeleteMapping("/deleteById/{id}")
    @ApiOperation("通过学院id删除学院")
    public Result<Object>  deleteById(@PathVariable("id") String id){
        Assert.notNull(id, new ParamsErrorException("id入参不能为null"));
        Assert.notBlank(id, new ParamsErrorException("id入参不能为空"));
        if (academyService.removeById(id))
            return Result.success();
        return Result.error(-1, "学院不存在");
    }
    @DeleteMapping("/deleteByName/{name}")
    @ApiOperation("通过学院名删除学院")
    public Result<Object>  deleteByName(@PathVariable("name") String name){
        Assert.notNull(name, new ParamsErrorException("name入参不能为null"));
        Assert.notBlank(name, new ParamsErrorException("name入参不能为空"));
        if (academyService.remove(new LambdaQueryWrapper<Academy>().eq(Academy::getAcademyName, name)))
            return Result.success();
        return Result.error(-1, "学院不存在");
    }
    @GetMapping("/getAcademyByName/{name}")
    @ApiOperation("通过学院名获取学院")
    public AcademyVO getAcademyByName(@PathVariable("name") String name){
        System.out.println("yes");
        Assert.notNull(name, new ParamsErrorException("name入参不能为null"));
        Assert.notBlank(name, new ParamsErrorException("name入参不能为空"));
        Academy academy = academyService.getOne(new LambdaQueryWrapper<Academy>().eq(Academy::getAcademyName, name));
        Assert.notNull(academy, new NotFoundException("学院不存在"));
        AcademyVO academyVo = new AcademyVO();
        BeanUtils.copyProperties(academy, academyVo);
        return academyVo;
    }

    @ApiOperation("通过学院级联到下面专业")
    @GetMapping("/jilianMajor")
    public Result jilianMajor(){
        List<Academy_Major_VO> result=new ArrayList<>();
        List<Academy> academies = academyService.list();
        List<Major> majors = iMajorService.list();
        for (int i=0;i<academies.size();i++){
            String academyId=academies.get(i).getAcademyId();
            String academyName=academies.get(i).getAcademyName();
            List<MajorVO2> majorVO2s=new ArrayList<>();
            for (int j=0;j<majors.size();j++){
                if(academyId.equals(majors.get(j).getAcademyId())){
                    majorVO2s.add(new MajorVO2(majors.get(j).getMajorCode(),majors.get(j).getMajorName()));
                }
            }
            result.add(new Academy_Major_VO(academyId,academyName,majorVO2s));
        }
        return Result.success(ResultCode.SUCCESS,result);
    }

}
